package com.store.controller;

import com.store.entity.User;
import com.store.service.UserService;
import com.store.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@Api("页面跳转")
@Controller
public class PageController {

    @DubboReference(interfaceClass = UserService.class,version = "1.0.0" ,timeout = 5000)
    private UserService userService;

    @ApiOperation("主页")
    @GetMapping({"/","/index"})
    public String toIndex(){
        return "index";
    }

    @GetMapping("/go/backstage")
    public String backstage(){
        return "admin/index";
    }


    @ApiOperation("查询所有用户，并返回")
    @GetMapping("/go/getAllUser")
    @ResponseBody
    public PageInfo<User> getUserList(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        PageInfo<User> pageInfo = userService.getAllUser(pn, 5);
        model.addAttribute("pageInfo",pageInfo);
//        return "admin/user_list";
        return pageInfo;
    }


    @ApiOperation("跳转到用户添加页面")
    @GetMapping("/go/toAddUser")
    public String toAddUser(){
        return "admin/user_add";
    }


    @ApiOperation("添加用户")
    @PostMapping("/go/addUser")
    public String addUser(User user){
        userService.addUser(user);
        return "redirect:/getAllUser";
    }


    @ApiOperation("删除用户")
    @DeleteMapping("/go/deleteUser/{uid}")
    public String deleteUser(@PathVariable("uid") Integer uid){
        userService.deleteUserById(uid);
        return "redirect:/getAllUser";
    }

    @ApiOperation("购物车")
    @GetMapping("/toCart")
    public String toCart(){
        return "cart";
    }

    @ApiOperation("订单")
    @GetMapping("/toOrder")
    public String toOrder(){
        return "orderConfirm";
    }

    @ApiOperation("支付")
    @GetMapping("/toPayment")
    public String toPayment(Integer price, Model model){
        model.addAttribute("totalPrice",price);
        return "payment";
    }

    @ApiOperation("支付成功")
    @GetMapping("/toPaymentSuccess")
    public String toPaymentSuccess(Integer price,Model model){
        model.addAttribute("totalPrice",price);
        return "paySuccess";
    }

    @ApiOperation("收获地址")
    @GetMapping("/toAddress")
    public String toAddress(){
        return "address";
    }

    @ApiOperation("新增收获地址")
    @GetMapping("/toAddAddress")
    public String toAddAddress(){
        return "addAddress";
    }


    @ApiOperation("修改密码")
    @GetMapping("/toUpdate")
    public String toUpdate(){
        return "password";
    }

    @ApiOperation("退出登录")
    @GetMapping("/user/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/web/login.html";
    }


}
